//
//  ArticleTextImgViewCell.m
//  yigou
//
//  Created by wangdianlei on 2017/1/18.
//  Copyright © 2017年 hanyiwang. All rights reserved.
//

#import "ArticleTextImgViewCell.h"
#import "PublishArticleViewController.h"
#import "PublishGoodViewController.h"
#import "ArticleModel.h"
#import "JCViewController.h"

@implementation ArticleTextImgViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.img = [[UIImageView alloc] initWithFrame:CGRectMake(12, 10, 100, 100)];
    self.img.backgroundColor = [UIColor colorWithHexString:@"E9E9F1"];
    self.img.contentMode = UIViewContentModeScaleAspectFill;
    self.img.image = [UIImage imageNamed:@"placeholder"];
    [self.contentView addSubview:self.img];
    self.img.layer.masksToBounds = YES;
    
    self.contentTextView = [[PlaceholderTextView alloc]initWithFrame:CGRectMake(112, 10, ScreenWidth- 124, 100)];
    self.contentTextView.delegate = self;
    self.contentTextView.font = [UIFont systemFontOfSize:16.f];
    self.contentTextView.textColor = [UIColor blackColor];
    self.contentTextView.textAlignment = NSTextAlignmentLeft;
    self.contentTextView.editable = YES;
    self.contentTextView.placeholderColor = [UIColor lightGrayColor];
    self.contentTextView.placeholder = @"添加图片描述";
    [self.contentView addSubview:self.contentTextView];
    self.contentTextView.backgroundColor = [UIColor colorWithHexString:@"E9E9F1"];
    
    CGFloat width = ScreenWidth-12 - 22;
    self.buttonmore = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonmore.frame = CGRectMake(width - 5, 110 - 22 - 5, 22, 22);
    [self.buttonmore setBackgroundImage:[UIImage imageNamed:@"cellmore"] forState:UIControlStateNormal];
    
    self.buttondel = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttondel.frame = CGRectMake(width - 5 - (22 + 10), 110 - 22 - 5, 22, 22);
    [self.buttondel setBackgroundImage:[UIImage imageNamed:@"celldel"] forState:UIControlStateNormal];
    
    self.buttondown = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttondown.frame = CGRectMake(width - 5 - (22 + 10) * 2, 110 - 22 - 5, 22, 22);
    [self.buttondown setBackgroundImage:[UIImage imageNamed:@"celldown"] forState:UIControlStateNormal];
    
    self.buttonup = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonup.frame = CGRectMake(width - 5 - (22 + 10) * 3, 110 - 22 - 5, 22, 22);
    [self.buttonup setBackgroundImage:[UIImage imageNamed:@"cellup"] forState:UIControlStateNormal];
    
    [self.buttonmore addTarget:self action:@selector(clickMore) forControlEvents:UIControlEventTouchUpInside];
    [self.buttondel addTarget:self action:@selector(clickDel) forControlEvents:UIControlEventTouchUpInside];
    [self.buttondown addTarget:self action:@selector(clickDown) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonup addTarget:self action:@selector(clickUp) forControlEvents:UIControlEventTouchUpInside];
    
    [self.contentView addSubview:self.buttonmore];
    [self.contentView addSubview:self.buttondel];
    [self.contentView addSubview:self.buttondown];
    [self.contentView addSubview:self.buttonup];
    self.buttondel.hidden = YES;
    self.buttondown.hidden = YES;
    self.buttonup.hidden = YES;
    
    [self addChooseTribe];
}

- (void)addChooseTribe {
    self.tagbg = [[UIView alloc] init];
    [self.contentView addSubview:self.tagbg];
    [self.tagbg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(12);
        make.right.equalTo(self.contentView.mas_right).offset(-12);
        make.height.mas_equalTo(30);
        make.bottom.equalTo(self.contentView.mas_bottom);
    }];
    self.tagbg.backgroundColor = [UIColor colorWithHexString:@"E9E9F1"];
    
    self.tagbg.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickChooseTribe)];
    tap.numberOfTapsRequired = 1;
    [self.tagbg addGestureRecognizer:tap];
    
//    UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth - 24, 1)];
//    line1.backgroundColor = LineColor;
//    [bg addSubview:line1];
    
    self.tagtitle = [[UILabel alloc] initWithFrame:CGRectMake(12, 1, ScreenWidth - 30 - 24 - 10, 28)];
    self.tagtitle.text = @"选择标签";
    self.tagtitle.font = [FontTool fontWithSize:15];
    self.tagtitle.textColor = [UIColor lightGrayColor];
    self.tagtitle.textAlignment = NSTextAlignmentLeft;
    [self.tagbg addSubview:self.tagtitle];
    
    UIImageView *arrow = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth - 30 - 24, 6, 18, 18)];
    arrow.image = [UIImage imageNamed:@"rightarrow"];
    [self.tagbg addSubview:arrow];
    
//    UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(0, 49, ScreenWidth - 24, 1)];
//    line2.backgroundColor = LineColor;
//    [bg addSubview:line2];
}

- (void)clickChooseTribe {
//    MyAddTribeListViewController *vc = [[MyAddTribeListViewController alloc] initWithNibName:@"MyAddTribeListViewController" bundle:nil];
//    vc.type = IMyAddTribeListChoose;
//    if (self.controller) {
//        [self.controller.navigationController pushViewController:vc animated:YES];
//    }
    if (self.controller) {
        JCViewController *view = [[JCViewController alloc]init];
        [self.controller.navigationController pushViewController:view animated:YES];
        [view returnText:^(NSString *showText) {
            self.tagtitle.textColor = [UIColor blackColor];
            self.tagtitle.font = [UIFont systemFontOfSize:15];
            self.tagtitle.text = showText;
            
            //self.labelArray  = [self.tribeDesc.text componentsSeparatedByString:@","];
        }];
    }
}

- (void)textViewDidChange:(UITextView *)textView {
    if (self.controller) {
        if ([self.controller isKindOfClass:[PublishArticleViewController class]]) {
            ArticleModel *model = [PublishData sharedPublishData].localArray[self.dataIndex];
            model.txt = textView.text;
        } else if ([self.controller isKindOfClass:[PublishGoodViewController class]]) {
            ArticleModel *model = [PublishData sharedPublishData].goodlocalArray[self.dataIndex];
            model.txt = textView.text;
        }
    }
}

- (void)clickUp {
    [self clickMore];
    if (self.dataIndex == 0) {
        return;
    }
    if (self.controller) {
        if ([self.controller isKindOfClass:[PublishArticleViewController class]]) {
            PublishArticleViewController *pub = (PublishArticleViewController *)self.controller;
            [pub updateTable:self.dataIndex newIndex:self.dataIndex - 1];
        } else if ([self.controller isKindOfClass:[PublishGoodViewController class]]) {
            PublishGoodViewController *pub = (PublishGoodViewController *)self.controller;
            [pub updateTable:self.dataIndex newIndex:self.dataIndex - 1];
        }
    }
}

- (void)clickDown {
    [self clickMore];
    if (self.dataIndex == [PublishData sharedPublishData].localArray.count - 1) {
        return;
    }
    if (self.controller) {
        if ([self.controller isKindOfClass:[PublishArticleViewController class]]) {
            PublishArticleViewController *pub = (PublishArticleViewController *)self.controller;
            [pub updateTable:self.dataIndex newIndex:self.dataIndex + 1];
        } else if ([self.controller isKindOfClass:[PublishGoodViewController class]]) {
            PublishGoodViewController *pub = (PublishGoodViewController *)self.controller;
            [pub updateTable:self.dataIndex newIndex:self.dataIndex + 1];
        }
    }
}

- (void)clickDel {
    [self clickMore];
    if (self.controller) {
        if ([self.controller isKindOfClass:[PublishArticleViewController class]]) {
            [[PublishData sharedPublishData].localArray removeObjectAtIndex:self.dataIndex];
            
            if (self.controller) {
                PublishArticleViewController *pb = (PublishArticleViewController *)self.controller;
                [pb refreshTable];
            }
        } else if ([self.controller isKindOfClass:[PublishGoodViewController class]]) {
            [[PublishData sharedPublishData].goodlocalArray removeObjectAtIndex:self.dataIndex];
            
            if (self.controller) {
                PublishGoodViewController *pb = (PublishGoodViewController *)self.controller;
                [pb refreshTable];
            }
        }
    }
}

- (void)clickMore {
    if ([self.buttondel isHidden]) {
        self.buttondel.hidden = NO;
        self.buttonup.hidden = NO;
        self.buttondown.hidden = NO;
    } else {
        self.buttondel.hidden = YES;
        self.buttonup.hidden = YES;
        self.buttondown.hidden = YES;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
